<?php
namespace Admin\Controller;
class AskController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('Ask');
        $this->_cate_mod = D('AskCate');
    }

    public function _before_index() {
		$this->list_relation = true;
        $p = I('p',1,'intval');
        $this->assign('p',$p);
    }

    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $cate_id = I('get.cate_id');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword,
        ));
        return $map;
    }



    public function _before_edit(){
        $id = I('get.id');
        $info = $this->_mod->field('id,cate_id,house_id')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$info['cate_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $info['cate_id'];
        }else{
            $spid .= $info['cate_id'];
        }

        if($info['house_id']){
            $house = M('house')->where(array('id'=>$info['house_id']))->find();
            $this->assign('house',$house);
        }

        $this->assign('selected_ids',$spid);
    }



}